//
//  BrandZoneViewController.m
//  FlyGift
//
//  Created by lanou3g on 15/7/15.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "BrandZoneViewController.h"
#import "BrandZoneTableViewCell.h"
#import "BrandZoneModel.h"
#define KBrandURL @"http://api.liwushuo.com/v2/brands/editor?limit=20&offset=" // 品牌专区首页

@interface BrandZoneViewController () <UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,assign)int offset;
@property(nonatomic,strong)NSMutableArray *brandModelArray;
@property(nonatomic,strong)UITableView *tableView;

@end

@implementation BrandZoneViewController

#pragma mark - 懒加载 - 
 - (NSMutableArray *)brandModelArray
{
    if (!_brandModelArray) {
        _brandModelArray = [NSMutableArray array];
    }
    return _brandModelArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addBrandTableView];
    [self brandZoneDownRefreshData];
    [self brandZoneUpRefreshData];
    [self addToTopButton];
}

#pragma mark - 添加表视图 - 
- (void)addBrandTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
}

#pragma mark - 返回分区数 - 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark - 返回每个分区组数 - 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.brandModelArray.count;
}

#pragma mark - 返回表视图的cell -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = @"brandZoneCell";
    [tableView registerClass:[BrandZoneTableViewCell class] forCellReuseIdentifier:cellIndentifier];
    BrandZoneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    BrandZoneModel *model = self.brandModelArray[indexPath.row];
    [cell setCellValueWithModel:model row:indexPath.row];
    return cell;
}

#pragma mark - 返回行高 - 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kP6width(270);
}

#pragma mark - 点击tableView跳转到品牌主页
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BrandZoneModel *model = self.brandModelArray[indexPath.row];
    if (self.brandZoneBlock != nil) {
        self.brandZoneBlock(model.cover_image_url,model.id);
    }
}


#pragma mark - 下拉刷新 - 
- (void)brandZoneDownRefreshData
{
    self.offset = 0;
    [self.tableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(tableViewDownRequset)];
    self.tableView.header.updatedTimeHidden = YES;
    NSMutableArray *idleImages = [NSMutableArray array];
    for (int i = 0; i < 2; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_loading_action%d@2x",i+1]];
        [idleImages addObject:image];
    }
    [self.tableView.gifHeader setImages:idleImages forState:MJRefreshHeaderStateIdle];
    NSMutableArray *refreshingImage = [NSMutableArray array];
    for (int i = 0; i < 2; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_loading_action%d@2x",i+1]];
        [refreshingImage addObject:image];
    }
    [self.tableView.gifHeader setImages:refreshingImage forState:MJRefreshHeaderStateRefreshing];
    [self.tableView.header beginRefreshing];
}

// 请求下拉数据
- (void)tableViewDownRequset
{
    if (self.brandModelArray) {
        [self.brandModelArray removeAllObjects];
        self.brandModelArray = nil;
    }
    NSString *url = [KBrandURL stringByAppendingFormat:@"%d",self.offset];
    [LORequestManger GET:url success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *dic2 = [dic valueForKey:@"data"];
        NSArray *array = [dic2 valueForKey:@"brands"];
        for (NSDictionary *obj in array) {
            BrandZoneModel *model = [BrandZoneModel brandZoneModelWithDictionary:obj];
            [self.brandModelArray addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        nil;
    }];
}

#pragma mark - 上拉加载 - 
- (void)brandZoneUpRefreshData
{
    [self.tableView addGifFooterWithRefreshingTarget:self refreshingAction:@selector(tableViewUpRequset)];
    NSMutableArray *refreshingImages = [NSMutableArray array];
    for (int i = 0; i < 2; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_loading_action%d@2x",i+1]];
        [refreshingImages addObject:image];
    }
    self.tableView.gifFooter.refreshingImages = refreshingImages;
}

// 请求上拉数据
- (void)tableViewUpRequset
{
    self.offset += 18;
    NSString *url = [KBrandURL stringByAppendingFormat:@"%d",self.offset];
    [LORequestManger GET:url success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *dic2 = [dic valueForKey:@"data"];
        NSArray *array = [dic2 valueForKey:@"brands"];
        for (NSDictionary *obj in array) {
            BrandZoneModel *model = [BrandZoneModel brandZoneModelWithDictionary:obj];
            [self.brandModelArray addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
        [self.tableView.footer noticeNoMoreData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        nil;
    }];
}

#pragma mark - 点击按钮回到顶部- 
-(void)addToTopButton
{
    UIButton * toTopButton = [UIButton buttonWithType:UIButtonTypeSystem];
    toTopButton.frame = CGRectMake(kScreenWidth - kP6width(60), kScreenHeight - kP6Height(250), kP6width(40), kP6width(40));
    [toTopButton setBackgroundImage:[UIImage imageNamed:@"home_back_top@3x"] forState:UIControlStateNormal];
    [toTopButton addTarget:self action:@selector(toTop) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:toTopButton];
}
-(void)toTop
{
    [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
    NSLog(@"回到顶部");
}

@end
